//
//  SELoginController.m
//  kuaiyi
//
//  Created by Alphts on 16/7/5.
//  Copyright © 2016年 Alphts. All rights reserved.
//

#import "SELoginController.h"
#import "NetworkTool.h"
#import "SEHomeController.h"
#import "SELeftController.h"

#define kUserName @"username"
@interface SELoginController ()

@property (nonatomic, weak) UITextField *username;
@property (nonatomic, weak) UITextField *password;


@end

@implementation SELoginController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.title = @"登录";

    [self setupUI];
}

-(void)setupUI{
    
    UIImageView *iconImage = [[UIImageView alloc]init];
    iconImage.image = [UIImage imageNamed:@"product_img_logo_nor.png"];
    
    
    UIImageView *nameImage = [[UIImageView alloc]init];
    nameImage.image = [UIImage imageNamed:@"product_text_logo_nor.png"];
    
    
    UITextField *userTextField = [[UITextField alloc]init];
    UITextField *passwordTextField = [[UITextField alloc]init];
    
    
    [userTextField setBorderStyle:UITextBorderStyleRoundedRect];
    [passwordTextField setBorderStyle:UITextBorderStyleRoundedRect];
    
    userTextField.placeholder = @"请输入用户名";
    passwordTextField.placeholder = @"请输入密码";
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setBackgroundColor:kThemeColor];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn setTitle:@"登   录" forState:UIControlStateNormal];
    
    
    [self.view addSubview:iconImage];
    [self.view addSubview:nameImage];
    [self.view addSubview:userTextField];
    [self.view addSubview:passwordTextField];
    [self.view addSubview:loginBtn];
    
    
    [iconImage makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.offset(100);
        make.centerX.equalTo(self.view.centerX);
        make.width.equalTo(@67);
        make.height.equalTo(@88);
    }];
    
    [nameImage makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(iconImage.bottom).offset(0);
        make.centerX.equalTo(self.view.centerX);
        make.width.equalTo(@83);
        make.height.equalTo(@49);
       
    }];
    
    [userTextField makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(nameImage.bottom).offset(30);
        make.centerX.equalTo(self.view.centerX);
        make.leading.equalTo(20);
        make.trailing.equalTo(-20);
        make.height.equalTo(@30);
    }];
    
    [passwordTextField makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(userTextField.bottom).offset(20);
        make.centerX.equalTo(self.view.centerX);
        make.leading.equalTo(20);
        make.trailing.equalTo(-20);
        make.height.equalTo(@30);
    }];
    
    [loginBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(passwordTextField.bottom).offset(30);
        make.centerX.equalTo(self.view.centerX);
        make.leading.equalTo(20);
        make.trailing.equalTo(-20);
        make.height.equalTo(@30);
    }];

    
    [loginBtn addTarget:self action:@selector(didClickLoginButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.username = userTextField;
    self.password = passwordTextField;
   
}


-(void)didClickLoginButton{
    
    if (self.username.text.length<=0 || self.password.text.length<=0) {
        [SVProgressHUD showInfoWithStatus:@"请输入用户名和密码"];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        return;
    }
    
    //MARK:判断网络状况
    //如果网络没有连接成功的话--->直接提示网络未连接成功 kNetWorkFailureStatus
    
    [SENetworkReachabilityTool checkNetworkWithReachableBlock:^{
        
        //正常的情况应该是把用户名和密码作为参数去访问网络服务器
        [[NetworkTool shareManager] POST:@"http://iosapi.itcast.cn/carelinkQuickLogin.json.php" parameters:@{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if ([self.username.text isEqualToString:@"37"] && [self.password.text isEqualToString:@"37"]) {
                
                
                //保存用户输入偏好设置
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                
                [defaults setObject:@"37" forKey:kUserName];
                [defaults setObject:@"37" forKey:@"password"];
                [defaults synchronize];
                
                [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [SVProgressHUD dismiss];
                    
                    //回调已登录界面
                    if (_returnBlk) {
                        _returnBlk();
                    }
                    //导航控制器push了,所以再pop一下
                    [[self centerNavigationController]popViewControllerAnimated:YES];
                });
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:@"账号或密码错误"];
            }
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [SVProgressHUD showErrorWithStatus:@"网络超时"];

        }];
    }];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}





@end
